package models

import (
	"time"
	"github.com/astaxie/beego/orm"
)

type Users struct {
	Id                  int
	UserName            string    `ht:"list;search;name(用户名)"`
	UserPhone           string    `ht:"list;name(手机号)"`
	Password            string    `ht:"name(密码)"`
	WxOpenId            string    `ht:"name(微信OpenId)"`
	WxToken             string    `ht:"name(微信昵称)"`
	Sex                 int       `ht:"name(性别);enum([{\"Value\":\"未知\",\"key\":\"0\"},{\"Value\":\"男\",\"key\":\"1\"},{\"Value\":\"女\",\"key\":\"1\"}])"`
	Province            string    `ht:"name(省份)"`
	City                string    `ht:"name(城市)"`
	Country             string    `ht:"name(国别)"`
	HeadImgURL          string    `ht:"name(微信头像地址)"`
	Unionid             string    `ht:"name(特殊唯一标识ID)"`
	CreateTime          time.Time `ht:"name(创建时间)"`
	UpdateTime          time.Time `ht:"list;name(最近抽奖时间)"`
	UserType            int       `ht:"list;name(用户类型);subclass([{\"Num\":0,\"Name\":\"普通用户\"},{\"Num\":1,\"Name\":\"商家\"},{\"Num\":10,\"Name\":\"服务台\"},{\"Num\":100,\"Name\":\"总服务台\"}])"`
	UserCurrentPoint    int64     `ht:"list;name(用户当前积分)"`
	UserLoseEffectPoint int       `ht:"list;name(失效积分)"`
	UserCommitPoint     int       `ht:"list;name(已兑换积分)"`
	UserTotalPoint      int64     `ht:"list;name(总计获取的积分)"`
	UserPlayPointTimes  int       `ht:"list;name(玩积分游戏次数)"`
	UserPlayCouponTimes int       `ht:"list;name(玩优惠券游戏次数)"`
}

func init() {
	orm.RegisterModel(new(Users))
}
func (*Users) TableName() string {
	return "users"
}

func (*Users) FN_Id() string {
	return "Id"
}
func (*Users) FN_UserName() string {
	return "UserName"
}
func (*Users) FN_UserPhone() string {
	return "UserPhone"
}
func (*Users) FN_WxOpenId() string {
	return "WxOpenId"
}
func (*Users) FN_WxToken() string {
	return "WxToken"
}

func (*Users) FN_Sex() string {
	return "Sex"
}
func (*Users) FN_Province() string {
	return "Province"
}
func (*Users) FN_City() string {
	return "City"
}
func (*Users) FN_Country() string {
	return "Country"
}
func (*Users) FN_HeadImgURL() string {
	return "HeadImgURL"
}
func (*Users) FN_Unionid() string {
	return "Unionid"
}

func (*Users) FN_CreateTime() string {
	return "CreateTime"
}
func (*Users) FN_UpdateTime() string {
	return "UpdateTime"
}
func (*Users) FN_UserType() string {
	return "UserType"
}
func (*Users) FN_UserCurrentPoint() string {
	return "UserCurrentPoint"
}
func (*Users) FN_UserLoseEffectPoint() string {
	return "UserLoseEffectPoint"
}
func (*Users) FN_UserCommitPoint() string {
	return "UserCommitPoint"
}
func (*Users) FN_UserTotalPoint() string {
	return "UserTotalPoint"
}
func (*Users) FN_UserPlayPointTimes() string {
	return "UserPlayPointTimes"
}
func (*Users) FN_UserPlayCouponTimes() string {
	return "UserPlayCouponTimes"
}

func UsersGetInfo(user *Users) error {
	o := orm.NewOrm()
	return o.Read(user)
}

func UsersGetInfoByWxOpenid(user *Users) error {
	o := orm.NewOrm()
	return o.Read(user, user.FN_WxOpenId())
}

func UsersInsert(user *Users) error {
	o := orm.NewOrm()
	_, err := o.Insert(user)
	return err
}

func UsersUpdate(user *Users) error {
	o := orm.NewOrm()
	_, err := o.Update(user, user.FN_WxOpenId())
	return err
}

func UsersExistByOpenid(openid string) bool {
	o := orm.NewOrm()
	m := Users{}
	return o.QueryTable(m.TableName()).Filter(m.FN_WxOpenId(), openid).Exist()
}

func UserCurrentPointUpdate(user *Users, changePoints int) error {
	o := orm.NewOrm()
	if changePoints > 0 {
		_, err := o.QueryTable(user.TableName()).Filter(user.FN_Id(), user.Id).Update(orm.Params{user.FN_UserCurrentPoint(): orm.ColValue(orm.ColAdd, changePoints)})
		return err
	} else {
		_, err := o.QueryTable(user.TableName()).Filter(user.FN_Id(), user.Id).Update(orm.Params{user.FN_UserCurrentPoint(): orm.ColValue(orm.ColMinus, -changePoints)})
		return err
	}
	return nil
}

func UserLoseEffectPointUpdate(user *Users, changePoints int) error {
	o := orm.NewOrm()
	if changePoints > 0 {
		_, err := o.QueryTable(user.TableName()).Filter(user.FN_Id(), user.Id).Update(orm.Params{user.FN_UserLoseEffectPoint(): orm.ColValue(orm.ColAdd, changePoints)})
		return err
	} else {
		_, err := o.QueryTable(user.TableName()).Filter(user.FN_Id(), user.Id).Update(orm.Params{user.FN_UserLoseEffectPoint(): orm.ColValue(orm.ColMinus, -changePoints)})
		return err
	}
	return nil
}

func UserTotalPointUpdate(user *Users, changePoints int) error {
	o := orm.NewOrm()
	if changePoints > 0 {
		_, err := o.QueryTable(user.TableName()).Filter(user.FN_Id(), user.Id).Update(orm.Params{user.FN_UserTotalPoint(): orm.ColValue(orm.ColAdd, changePoints)})
		return err
	} else {
		_, err := o.QueryTable(user.TableName()).Filter(user.FN_Id(), user.Id).Update(orm.Params{user.FN_UserTotalPoint(): orm.ColValue(orm.ColMinus, -changePoints)})
		return err
	}
	return nil
}

func UserPlayPointTimesAdd(user *Users) error {
	o := orm.NewOrm()
	_, err := o.QueryTable(user.TableName()).Filter(user.FN_Id(), user.Id).Update(orm.Params{user.FN_UserPlayPointTimes(): orm.ColValue(orm.ColAdd, 1)})
	return err
}

func UserPlayCouponTimesAdd(user *Users) error {
	o := orm.NewOrm()
	_, err := o.QueryTable(user.TableName()).Filter(user.FN_Id(), user.Id).Update(orm.Params{user.FN_UserPlayCouponTimes(): orm.ColValue(orm.ColAdd, 1)})
	return err
}

func UsersGetOneByOffset(user *Users, offset int) error {
	o := orm.NewOrm()
	return o.QueryTable(user.TableName()).Offset(offset).Limit(1).One(user)
}
